package mylab.starters.generator.table.config;

import lombok.RequiredArgsConstructor;
import mylab.starters.generator.table.StarupMode;
import mylab.starters.generator.table.TableTemplate;
import mylab.starters.generator.table.service.SysMysqlCreateTableManager;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;


@Configuration
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@MapperScan("**.mapper.**")
@EnableConfigurationProperties({TableGeneratorProperties.class})
public class TableGeneratorAutoConfiguration {

    private final TableGeneratorProperties properties;

    private final SysMysqlCreateTableManager mysql;

    @Bean
    public TableTemplate tableTemplate() {

        return new TableTemplate() {
            @Override
            public boolean existTable(String tableName) {
                return mysql.existTable(tableName);
            }

            @Override
            public void generateTable(StarupMode starupMode, Class<?> entityClass, String tableName) {
                mysql.createMysqlTable(starupMode, entityClass, tableName);
            }

            @Override
            public void generateTable(StarupMode starupMode, String scanPack) {
                mysql.createMysqlTable(starupMode, scanPack);
            }
        };

    }

}
